package SingleMachineArrangeImpl;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

//Delay Artifact Count 误工工件数问题
public class DAC extends SingleMachineArrange{
    List<Artifact> removed_artifacts;
    @Override
    public void solve() {
        artifacts = new ArrayList<>();
        removed_artifacts = new ArrayList<>();
        if(weight==null&&due==null){
            weight = new int[]{4, 2, 5, 7, 6};
            due = new int[]{1, 2, 2, 3, 3};
        }
        for (int i = 0; i < due.length; i++) {
            Artifact a = new Artifact();
            a.process=(process==null)?1:process[i];
            a.weight = (weight==null)?1:weight[i];
            a.due = due[i];
            artifacts.add(a);
        }
        artifacts.sort(Comparator.comparingInt(artifact -> artifact.due));
        int k;
        while ((k=getFirstDelayedArtifact(artifacts))!=-1){
            int minw = Integer.MAX_VALUE;
            int iminw = -1;
            for (int i = 0; i <= k; i++) {
                Artifact artifact = artifacts.get(i);
                if(artifact.weight < minw){
                    minw = artifact.weight;
                    iminw = i;
                }
            }
            removed_artifacts.add(artifacts.get(iminw));
            artifacts.remove(iminw);
        }
        System.out.println("当前得到排序： \n正常交付：" + artifacts);
        System.out.println("误工工件：" + removed_artifacts);
        System.out.println("误工工件数：" + removed_artifacts.size());
    }

    //找出第一个误工工件，-1表示无误工工件
    private int getFirstDelayedArtifact(List<Artifact> artifacts){
        int t = 0;
        for (int i = 0; i < artifacts.size(); i++) {
            Artifact artifact = artifacts.get(i);
            t += artifact.process;
            if(artifact.due < t) return i;
        }
        return -1;
    }

}
